package com.bling.service;

import java.io.Serializable;
import java.sql.SQLException;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bling.common.bean.Area;
import com.bling.common.idao.IAreaDao;
import com.bling.common.page.PageResult;
import com.bling.iservice.IAreaService;
/**
 * 地区服务
 * @author chen-zhenxing
 *
 */
@Service
public class AreaService implements IAreaService {

	public void deleteArea(Area area) throws SQLException {
		areaDao.delete(area);
	}

	public void deleteAreaById(Serializable id) throws SQLException {
		areaDao.delete(Area.class, id);
	}

	public Area findAreaById(Serializable id) throws SQLException {
		return areaDao.find(Area.class, id);
	}

	public PageResult<Area> listAreaAll() throws SQLException {
		String hql = "from Area a";
		return areaDao.listAll(hql);
	}

	public PageResult<Area> listAreaByPage(int from, int num)
			throws SQLException {
		String hql = "from Area a order by a.sort desc";
		return areaDao.pageList(hql, from, num);
	}

	public void saveArea(Area area) throws SQLException {
		areaDao.save(area);
	}

	public void updateArea(Area area) throws SQLException {
		areaDao.update(area);
	}

	public void updateAreaSort(Serializable id, Integer sort)
			throws SQLException {
		String hql = "update Area a set a.sort=? where a.id=?";
		areaDao.update(hql,new Object[]{sort,id});
	}
	
	public PageResult<Area> searchArea(String keyword,int from,int num) throws SQLException{
		String hql = "from Area a where a.name like '%"+keyword+"%' order by a.sort desc";
		return areaDao.pageList(hql, from, num);
	}

	@Resource
	private IAreaDao areaDao;
	public IAreaDao getAreaDao() {
		return areaDao;
	}
	public void setAreaDao(IAreaDao areaDao) {
		this.areaDao = areaDao;
	}
}
